package com.feng.test.ppt;

import com.alibaba.fastjson.JSON;
import org.apache.poi.xslf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.UUID;


public class PPTX {

    public static void main(String[] args) {
        File file = new File("E:\\ppt\\wawa1.pptx");
        try (InputStream is = new FileInputStream(file)) {
            XMLSlideShow slideShow = new XMLSlideShow(is);

            List<XSLFSlide> slideList = slideShow.getSlides();

            for (XSLFSlide slide : slideList) {
                List<XSLFShape> shapeList = slide.getShapes();
                for (XSLFShape shape : shapeList) {
                    if (shape instanceof XSLFAutoShape) {
                        XSLFAutoShape autoShape = (XSLFAutoShape) shape;
                        Rectangle2D rectangle2D = autoShape.getAnchor();
                        System.out.println("X: " + rectangle2D.getX());
                        System.out.println("Y: " + rectangle2D.getY());
                        System.out.println(rectangle2D.toString());
                    }
                    // XSLFGraphicFrame(图表) XSLFConnectorShape（形状）XSLFGraphicFrame（组织）
                    //XSLFPictureShape （图片） XSLFTextBox（文本 继承autoShape） XSLFTable（表格）
                    //XSLFAutoShape（标题 ）
                    if (shape instanceof XSLFGraphicFrame) {
                        //XSLFPictureShape pictureShape = (XSLFPictureShape) shape;
                        XSLFGraphicFrame frame = (XSLFGraphicFrame) shape;
                        Rectangle2D rectangle2D = frame.getAnchor();


                        File file1 = new File("E:\\ppt\\" + UUID.randomUUID() + ".jpg");

                        BufferedImage img = new BufferedImage((int) rectangle2D.getWidth(), (int) rectangle2D.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D graphics = img.createGraphics();
                        graphics.setBackground(Color.white);
                        graphics.fill(new Rectangle2D.Float(0, 0, (int) rectangle2D.getWidth() , (int)rectangle2D.getHeight()));
                        frame.draw(graphics, rectangle2D);

                        try (FileOutputStream fos = new FileOutputStream(file1)) {
                            ImageIO.write(img, "jpg", fos);
                        }
                        img.flush();

                        XSLFPictureShape pictureShape = frame.getFallbackPicture();
                        if (pictureShape != null) {
                            XSLFPictureData pictureData = pictureShape.getPictureData();

                            try (
                                    InputStream inputStream = pictureData.getInputStream();
                                    FileOutputStream fos = new FileOutputStream(file1)
                            ) {
                                byte[] b = new byte[1024];
                                int len = 0;
                                while ((len = inputStream.read(b)) != -1) {
                                    fos.write(b, 0, len);
                                }
                            }
                        }

                    }


                    if (shape instanceof XSLFPictureShape) {
                        XSLFPictureShape pictureShape = (XSLFPictureShape) shape;
                        Rectangle2D rectangle2D = pictureShape.getAnchor();
                        int i = 10;
                        File file1 = new File("E:\\ppt\\wawa" + ++i + ".jpg");

                        BufferedImage img1 = new BufferedImage((int) rectangle2D.getWidth(), (int) rectangle2D.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D graphics1 = img1.createGraphics();
                        graphics1.setBackground(Color.white);
                        graphics1.fill(new Rectangle2D.Float(0, 0, (int) rectangle2D.getWidth() , (int)rectangle2D.getHeight()));
                        pictureShape.draw(graphics1, rectangle2D);
                        try (FileOutputStream fos = new FileOutputStream(file1)) {
                            ImageIO.write(img1, "jpg", fos);
                        }
                    }
                    System.out.println("slide shape " + shape.getClass());
                }
                Rectangle2D rectangle2D =new Rectangle2D.Float(0,0,100,100);
                File file2 = new File("E:\\ppt\\" + UUID.randomUUID() + ".jpg");
                BufferedImage img = new BufferedImage((int) rectangle2D.getWidth(), (int) rectangle2D.getHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setBackground(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, (int) rectangle2D.getWidth() , (int)rectangle2D.getHeight()));
                slide.draw(graphics);

                try (FileOutputStream fos = new FileOutputStream(file2)) {
                    ImageIO.write(img, "jpg", fos);
                }
                img.flush();


                System.out.println();
                //System.out.println("title  " + slide.getTitle());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
